package com.test.shop.dao.jdbc;

import com.test.shop.dao.BaseDAO;
import com.test.shop.dao.UserDAO;
import com.test.shop.pojo.User;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 徒有琴
 */
public class UserDAOImpl implements UserDAO {
    @Override
    public int addUser(String mobile, String password) {
        BaseDAO baseDAO = new BaseDAO();
        String sql = "insert into user (mobile,password) values(?,?)";
        List params = new ArrayList();
        params.add(mobile);//按照sql语句中的顺序把参数放进去
        params.add(password);
        try {
            return baseDAO.executeUpdate(sql, params);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            baseDAO.close();
        }
        return 0;
    }

    @Override
    public User getUserByMobile(String mobile) {
        BaseDAO baseDAO = new BaseDAO();
        String sql = "select id,mobile,password from user where mobile=?";
        List params = new ArrayList();
        params.add(mobile);//按照sql语句中的顺序把参数放进去
        try {
            ResultSet rs = baseDAO.executeQuery(sql, params);
            if (rs.next()) {
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setMobile(rs.getString("mobile"));
                user.setPassword(rs.getString("password"));
                return user;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            baseDAO.close();
        }
        return null;
    }
}
